#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : PythonProject
@File : portscan.py
@Author : donc
@Time : 2025/5/6 20:28  
@脚本说明 : 
    多线程端口盲扫
"""
import socket
from threading import Thread
from queue import Queue  # 添加线程安全队列


def scan(host, port,result_queue):
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_socket.settimeout(2)
    tcp_socket.connect((host, port))
    result_queue.put(port)
    tcp_socket.close()
    tcp_socket.close()

def ports_scan(host):
    result_queue = Queue()  # 创建线程安全队列
    ports=[7, 21, 22, 23, 25, 43, 53, 67, 68, 69, 79, 80, 81, 88, 109, 110, 113, 119, 123, 135, 137, 138, 139,
             143, 161, 162, 179, 194, 220, 389, 443, 445, 465, 513, 520, 546, 547, 554, 563, 631, 636, 991, 993,
             995, 1080, 1159,1162,1164,1194, 1433, 1434, 1494, 1521, 1701, 1723, 1755, 1812, 1813, 1863, 3269, 3306, 3307, 3389, 3544,
             4369, 5060, 5061, 5355, 5432, 5671, 5672, 6379, 7001, 8080, 8081, 8088, 8443, 8883, 8888, 9443, 9859,9860,9988,
             15672, 50389, 50636, 61613, 61614]
    thread_list=[]
    for port in ports:
        t=Thread(target=scan,args=(host,port,result_queue))
        t.start()
        thread_list.append(t)
    for t in thread_list:
        t.join()
        # 从队列中收集结果
        unique_ports = []
        while not result_queue.empty():
            unique_ports.append(result_queue.get())
        return sorted(unique_ports)  # 返回排序后的结果
